package com.LeetCode.Tree;

import org.junit.Test;

import javax.management.Query;
import java.util.*;

/**
 * 二叉树的右视图
 */
public class RightSideTreeView {

    public List<Integer> rightSideView(TreeNode root) {
        if(root == null) return null;
        Queue<TreeNode> queue = new LinkedList<>();
        List<Integer> res = new ArrayList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            int size = queue.size();
            for(int i = 0; i<size; i++){
                TreeNode node = queue.poll();
                if(node.left!=null) queue.offer(node.left);
                if(node.right!=null) queue.offer(node.right);
                if(i == size - 1) res.add(node.val);
            }
        }
        return res;
    }

    @Test
    public void test(){
        TreeNode node1 = new TreeNode(1);
        TreeNode node2 = new TreeNode(2);
        TreeNode node3 = new TreeNode(3);
        TreeNode node4 = new TreeNode(4);
        TreeNode node5 = new TreeNode(5);
        node1.left = node2;
        node2.right = node5;
        node1.right = node3;
        node3.right = node4;
        System.out.println(rightSideView(node1));
    }
}
